package org.opsli.modulars.business.iot.protocol;

import javax.xml.bind.DatatypeConverter;
import java.io.*;

public class ReadF {

    public String testDataInputStream(String filepath) throws IOException {
        File file = new File(filepath);
        DataInputStream din = new DataInputStream(new FileInputStream(file));
        StringBuilder hexData = new StringBuilder();
        byte temp = 0;
        for(int i=0;i<file.length();i++) {
            temp = din.readByte();
            // 以十六进制的无符号整数形式返回一个字符串表示形式。
            String str = Integer.toHexString(temp);
            if(str.length() == 8) {//去掉补位的f
                str = str.substring(6);
            }
            if(str.length() == 1) {
                str = "0"+str;
            }
            hexData.append(str.toUpperCase());
        }
        return hexData.toString();
    }
    public static void main(String[] args) throws IOException {
        String filepath = "E:\\source\\99.bin";
        ReadF test = new ReadF();
        String s = test.testDataInputStream(filepath);

        FileOutputStream fout = new FileOutputStream("E:\\b.txt");//将十六进制数存成文本
        fout.write(s.getBytes());
        fout.close();
    }
}
